package com.genghis.ptas.classify.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.dao.ClassifyDao;
import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.note.entity.Note;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Repository("classifyDao")
public class ClassifyDaoImpl extends PageDao implements ClassifyDao {

    @Override
    public Page<Type> findTypePageList(PageParameter pageParameter, Type type) {
        StringBuilder sql = new StringBuilder("SELECT * FROM type where 1=1 ");
        if (type.getTypeName() != null && !"".equals(type.getTypeName())) {
            sql.append(" AND type_name like '%").append(type.getTypeName().trim()).append("%'");
        }
        if (type.getMemo() != null && !"".equals(type.getMemo())) {
            sql.append(" AND memo like '%").append(type.getMemo().trim()).append("%'");
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(type), new BeanPropertyRowMapper<Type>(Type.class));
    }

    @Override
    public Boolean checkRepeatTypeName(String typeName) {
        String sql = "select * from type t where t.type_name =?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Type>(Type.class), typeName).isEmpty();
    }

    @Override
    public void addType(Type type) {
        String sql = "INSERT INTO type(type_name, memo) VALUES(:typeName, :memo)";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(type));
    }

    @Override
    public Type findTypeByTypeName(String typeName) {
        String sql = "select * from type t where t.type_name ='" + typeName + "'";
        return getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<Type>(Type.class));
    }

    @Override
    public void editType(Type type) {
        String sql = "UPDATE type SET type_name = :typeName , memo = :memo WHERE type_name = :oldTypeName";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(type));
    }

    @Override
    public void delType(String typeName) {
        String sql = "DELETE FROM type WHERE type_name = ?";
        getJdbcTemplate().update(sql, typeName);
    }

    @Override
    public void delTypeValues(String typeName) {
        String sql = "DELETE FROM type_values WHERE type_name = ?";
        getJdbcTemplate().update(sql, typeName);
    }

    @Override
    public List<TypeValues> findTypeValuesByTypeName(String typeName) {
        String sql = "select * from type_values where type_name=?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<TypeValues>(TypeValues.class), typeName);
    }

    @Override
    public Page<TypeValues> findTypeValuePageList(PageParameter pageParameter, TypeValues typeValue) {
        StringBuilder sql = new StringBuilder("SELECT * FROM type_values where 1=1 ");
        if (null != typeValue.getTypeName() && !"".equals(typeValue.getTypeName())) {
            sql.append(" AND type_name ='").append(typeValue.getTypeName()).append("'");
        }
        if (null != typeValue.getTypeValue() && !"".equals(typeValue.getTypeValue())) {
            sql.append(" AND type_value ='").append(typeValue.getTypeValue()).append("'");
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(typeValue), new BeanPropertyRowMapper<TypeValues>(TypeValues.class));
    }

    @Override
    public void addTypeValue(TypeValues typeValue) {
        String sql = "INSERT INTO type_values(type_name, type_value) VALUES(:typeName, :typeValue)";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(typeValue));
    }

    @Override
    public Boolean checkRepeatTypeValue(TypeValues typeValue) {
        String sql = "select * from type_values t where t.type_name=:typeName and t.type_value =:typeValue";
        return getNamedParameterJdbcTemplate().query(sql, new BeanPropertySqlParameterSource(typeValue), new BeanPropertyRowMapper<TypeValues>(TypeValues.class)).isEmpty();
    }

    @Override
    public void editTypeValue(TypeValues typeValues) {
        String sql = "UPDATE type_values SET  type_name = :typeName , type_value = :typeValue WHERE id = :id";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(typeValues));
    }

    @Override
    public void delTypeValue(TypeValues typeValues) {
        String sql = "DELETE FROM type_values WHERE id = ? ";
        getJdbcTemplate().update(sql, typeValues.getId());
    }

    @Override
    public TypeValues findTypeValuesByNameAndCode(TypeValues typeValues) {
        String sql = "select * from type_values where type_name=:typeName ";
        return getNamedParameterJdbcTemplate().queryForObject(sql, new BeanPropertySqlParameterSource(typeValues), new BeanPropertyRowMapper<TypeValues>(TypeValues.class));
    }

    @Override
    public TypeValues findTypeValuesByTypeValue(TypeValues typeValues) {
        String sql = "select * from type_values where type_value=:typeValue";
        return getNamedParameterJdbcTemplate().queryForObject(sql, new BeanPropertySqlParameterSource(typeValues), new BeanPropertyRowMapper<TypeValues>(TypeValues.class));
    }

    @Override
    public TypeValues findTypeValuesById(Integer id) {
        String sql = "select * from type_values where id=" + id;
        TypeValues typeValues = getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<TypeValues>(TypeValues.class));
        return typeValues;
    }

    @Override
    public List<Type> findTypeList() {
        String sql = "SELECT * FROM type where 1=1 ";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Type>(Type.class));
    }

    @Override
    public List<TypeValues> findAllTypeValueList() {
        String sql = "SELECT * FROM type_values where 1=1 ";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<TypeValues>(TypeValues.class));
    }

}
